//
//  FloatLayer.m
//  PlayBall
//
//  Created by yanxun on 8/23/14.
//
//

#import "FloatLayer.h"
#define BtnWidth 72.0f
#define BtnHeight 24.0f

#define CellHeight 37.5f

@interface FloatLayer ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>{
    UIView *_floatLayer;
    NSMutableArray *_btnArr;
    FloatLayerTag _layerTag;
    NSInteger _selectIndex;
    
    
    UITableView *_tab;
    NSMutableArray *_infoArr;
}

@end

@implementation FloatLayer

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        
    }
    return self;
}

- (id)initWithFloatLayer:(NSInteger)layerTag withLayerCallBack:(FloatLayerCallBack)floatLayer{
    self = [super init];
    if (self) {
        // Initialization code
        _layerTag = layerTag;
        self.floatLayerCallBack = floatLayer;
        
        [self initProperty];
    }
    return self;
}

- (id)initWithFloatLayer:(NSInteger)layerTag withLayerCQSSCallBack:(FloatLayerCQSSCallBack)floatCQSSLayer{
    self = [super init];
    if (self) {
        // Initialization code
        _layerTag = layerTag;
        self.floatLayerCQSSCallBack = floatCQSSLayer;
        
        [self initProperty];
    }
    return self;
}

- (void)initProperty{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backgroundColor = [UIColor blackColor];
    //self.alpha = 0.5f;
    //self.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5f];
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    
    [singleTap setNumberOfTapsRequired:1];//1次点击
    [singleTap setNumberOfTouchesRequired:1];//1个手指操作
    [singleTap setDelegate:self];
    
    [self addGestureRecognizer:singleTap];
}

-(void)handleSingleTap:(UIPanGestureRecognizer *)recognizer{
    [self removeFloatLayer];
}


#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 输出点击的view的类名
    NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

- (void)loadFloatLayer:(NSArray *)arr withIndex:(NSInteger)index{
    
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    
    NSInteger count = arr.count;
    if (!_floatLayer) {
        _floatLayer = [[UIView alloc] init];
        _floatLayer.backgroundColor = [UIColor whiteColor];
        _floatLayer.layer.borderWidth = 0;
        _floatLayer.layer.borderColor = [[UIColor colorWithRed:186/255 green:186/255 blue:186/255 alpha:0.11] CGColor];//设置边框的颜色
        _floatLayer.layer.masksToBounds = NO;
        _floatLayer.layer.shadowOffset = CGSizeMake(.0,.0);
        _floatLayer.layer.shadowRadius = 0;
        _floatLayer.layer.shadowColor = [UIColor blackColor].CGColor;
        _floatLayer.layer.shadowOpacity = 0.3;
        _floatLayer.layer.cornerRadius = 5;
    }
    [self addSubview:_floatLayer];
    
    if (FloatLayerList == _layerTag) {
        
        _selectIndex = index;
        _floatLayer.layer.cornerRadius = 2;
        //用于列表浮层
        if (!_infoArr) {
            _infoArr = [[NSMutableArray alloc] initWithArray:arr];
        }else{
            [_infoArr removeAllObjects];
            [_infoArr addObjectsFromArray:arr];
        }
        
        
        CGFloat tabHeight = CellHeight*count;
        CGFloat tabWidth = 106.0f;
        if (count>5) {
            tabHeight = CellHeight*5;
        }
        
        _floatLayer.frame = CGRectMake(158.0f, 108.0f, tabWidth, tabHeight);
        
        
        if (!_tab) {
            //NSLog(@"height = %f",self.view.frame.size.height);
            _tab = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tabWidth, tabHeight)
                                                style:UITableViewStylePlain];
            
        }
        _tab.dataSource = self;
        _tab.delegate = self;
        _tab.backgroundView = nil;
        _tab.userInteractionEnabled = YES;
        _tab.scrollEnabled = YES;
        _tab.backgroundColor = [UIColor clearColor];
        [_tab setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
        _tab.tableFooterView = v;
        [_tab setSeparatorColor:SeparatorLineColor];
        if (IOS7) {
            [_tab setSeparatorInset:UIEdgeInsetsZero];
        }
        [_floatLayer addSubview:_tab];
        [_tab reloadData];
    }else{
        //用于按钮浮层
        
        if (!_btnArr) {
            _btnArr = [[NSMutableArray alloc] init];
        }
        [_btnArr removeAllObjects];
        
        for (id view in _floatLayer.subviews) {
            [view removeFromSuperview];
        }
        
        CGFloat floatLayerWidth = 0.0f;
        CGFloat floatLayerHeight = 0.0f;
        CGFloat offX = 14.0f;
        CGFloat offY = 8.0f;
        CGFloat offXWidth = 20.0f;
        CGFloat offYHeight = 15.0f;
        if (count > 3) {
            NSInteger row = (count-1)/3;
            floatLayerWidth = 14*2 + BtnWidth*3 + 20*2;
            floatLayerHeight = 8*2 + BtnHeight*(row+1) + 15*row;
            _floatLayer.frame = CGRectMake((SCREEN_WIDTH-floatLayerWidth)/2.0f, 80, floatLayerWidth, floatLayerHeight);
            
        }else if ((count>0) && (count<=3)){
            floatLayerWidth = 14*2 + BtnWidth*count + 20*2;
            floatLayerHeight = 8*2 + BtnHeight;
            _floatLayer.frame = CGRectMake((SCREEN_WIDTH-floatLayerWidth)/2.0f, 80, floatLayerWidth, floatLayerHeight);
            
        }else{
            floatLayerWidth = 0.0f;
            floatLayerHeight = 0.0f;
        }
        
        //加载多个按钮
        for (NSInteger i=0; i<count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = [[UIColor colorWithRed:226.0f/255.0f green:226.0f/255.0f blue:226.0f/255.0f alpha:1.0f] CGColor];//设置边框的颜色
            btn.layer.masksToBounds = NO;
            btn.layer.cornerRadius = 5;
            btn.frame = CGRectMake(offX + (BtnWidth+offXWidth)*(i%3), offY+i/3*(BtnHeight+offYHeight), BtnWidth, BtnHeight);
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            btn.backgroundColor = [UIColor colorWithRed:247.0f/255.0f green:247.0f/255.0f blue:247.0f/255.0f alpha:1.0f];
            [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithRed:99.0f/255.0f green:99.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
            
            if (index == i) {
                btn.layer.borderColor = [[UIColor colorWithRed:250.0f/255.0f green:136.0f/255.0f blue:140.0f/255.0f alpha:1.0f] CGColor];//设置边框的颜色
                _selectIndex = index;
            }
            [_floatLayer addSubview:btn];
            [_btnArr addObject:btn];
        }
    }
}

- (void)removeFloatLayer{
    [self removeFromSuperview];
}

- (void)click:(id)sender{
    NSInteger tag = [(UIButton *)sender tag];
    
    if (tag != _selectIndex) {
        for (id view in _btnArr) {
            [view setImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
        }
        
        
        [(UIButton *)sender setImage:[UIImage imageNamed:@"buttonSelected"] forState:UIControlStateNormal];
        //    if ((FloatLayerTogetherBuy == _layerTag)) {
        //        _floatLayerCallBack(tag);
        //    }
        
        if (FloatLayerCQSS == _layerTag) {
            //重庆时时彩
            if ([[((UIButton *)sender).titleLabel text] length]>4) {
                self.first = [[((UIButton *)sender).titleLabel text] substringToIndex:4];
                self.last = [[((UIButton *)sender).titleLabel text] substringFromIndex:4];
            }else{
                self.first = [[((UIButton *)sender).titleLabel text] substringToIndex:2];
                self.last = [[((UIButton *)sender).titleLabel text] substringFromIndex:2];
            }
            _floatLayerCQSSCallBack(tag,self.first,self.last);
        }else{
            _floatLayerCallBack(tag);
        }
        
        [self removeFloatLayer];
    }
    _selectIndex = tag;
}




#pragma mark UITableViewDelegate
#pragma mark -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return [[_registerInfoDic objectForKey:[NSString stringWithFormat:@"userInfo%d",section]] count];
    return [_infoArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"SchemeListTabStr";
    UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr] ;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    
    cell.backgroundColor = [UIColor clearColor];
    if (_selectIndex == [indexPath row]) {
        cell.backgroundColor = UIColorRef(215.0f, 215.0f, 215.0f);
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    cell.textLabel.text = [_infoArr objectAtIndex:[indexPath row]];
    CGRect selfRect = cell.frame;
    selfRect.size.height = CellHeight;
    cell.frame = selfRect;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    _floatLayerCallBack([indexPath row]);
    [self removeFloatLayer];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
